import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:winkai/entity/chat_topic.dart';
import 'package:winkai/ext/ext.dart';
import 'package:winkai/ext/screen_ext.dart';
import 'package:winkai/helper/service_conf_helper.dart';
import 'package:winkai/res/dimens.dart';
import 'package:winkai/utils/image_utils.dart';

void showChatTopicPop(
  BuildContext context, {
  required void Function(String) onSendMessage,
}) {
  showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      barrierColor: Colors.transparent,
      builder: (context) {
        return ChatTopicWidget(
          onSendMessage: onSendMessage,
        );
      });
}

class ChatTopicWidget extends StatefulWidget {
  const ChatTopicWidget({
    super.key,
    required this.onSendMessage,
  });

  final void Function(String)? onSendMessage;

  @override
  State<ChatTopicWidget> createState() => _ChatTopicWidgetState();
}

class _ChatTopicWidgetState extends State<ChatTopicWidget> {
  void Function(String)? onSendMessage;
  final List<ChatTopic> _topicList = [];

  @override
  void initState() {
    super.initState();
    onSendMessage = widget.onSendMessage;
    _topicList.addAll(_getTopicList());
  }

  List<ChatTopic> _getTopicList() {
    List<ChatTopic> topicList = [];
    if (!ServiceConfHelper().isCleanMode) {
      topicList.add(
        ChatTopic(
            name: 'chat_topic_photo'.tr,
            assetIcon: 'ic_ai_chat_topic_photo',
            list: 'chat_topic_photo_value'.tr.toStringList ?? [],
            textColor: const Color(0xffB670F0)),
      );
      topicList.add(
        ChatTopic(
            name: 'chat_topic_nsfw'.tr,
            assetIcon: 'ic_ai_chat_topic_nsfw',
            list: 'chat_topic_nsfw_value'.tr.toStringList ?? [],
            textColor: const Color(0xffF58E3B)),
      );
      topicList.add(
        ChatTopic(
            name: 'chat_topic_roleplay'.tr,
            assetIcon: 'ic_ai_chat_topic_roleplay',
            list: 'chat_topic_roleplay_value'.tr.toStringList ?? [],
            textColor: const Color(0xff91D743)),
      );
    }
    topicList.add(
      ChatTopic(
          name: 'chat_topic_movies'.tr,
          assetIcon: 'ic_ai_chat_topic_movies',
          list: 'chat_topic_movies_value'.tr.toStringList ?? [],
          textColor: const Color(0xffB670F0)),
    );
    topicList.add(
      ChatTopic(
          name: 'chat_topic_sports'.tr,
          assetIcon: 'ic_ai_chat_topic_sports',
          list: 'chat_topic_sports_value'.tr.toStringList ?? [],
          textColor: const Color(0xffF58E3B)),
    );
    topicList.add(
      ChatTopic(
          name: 'chat_topic_love'.tr,
          assetIcon: 'ic_ai_chat_topic_love',
          list: 'chat_topic_love_value'.tr.toStringList ?? [],
          textColor: const Color(0xff91D743)),
    );
    return topicList;
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Get.back();
      },
      behavior: HitTestBehavior.translucent,
      child: Padding(
        padding: EdgeInsets.only(bottom: navigationHeight + 96.w),
        child: Container(
          height: 50.w,
          padding: EdgeInsets.symmetric(vertical: 12.w),
          alignment: Alignment.center,
          child: ListView.separated(
            physics: const BouncingScrollPhysics(),
            scrollDirection: Axis.horizontal,
            padding:
                EdgeInsets.symmetric(horizontal: Dimens.mainHorizontalSpacing),
            itemBuilder: (context, index) {
              return _buildItem(_topicList[index]);
            },
            separatorBuilder: (context, index) => SizedBox(width: 8.w),
            itemCount: _topicList.length,
          ),
        ),
      ),
    );
  }

  Widget _buildItem(ChatTopic item) {
    return GestureDetector(
      onTap: () {
        onSendMessage?.call(item.list.randomOrNull ?? '');
        // reportEvent('c_chat_topic_${item.topicName}');
        Get.back();
      },
      child: Container(
        height: 26.w,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(22.w),
          color: Colors.white,
        ),
        alignment: Alignment.centerLeft,
        padding: EdgeInsets.symmetric(horizontal: 12.w),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            ImageUtils.getImage(item.assetIcon, 16.w, 16.w),
            SizedBox(width: 1.5.w),
            Text(
              item.name ?? '',
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              textAlign: TextAlign.center,
              style: TextStyle(
                  fontSize: 13.sp,
                  color: item.textColor,
                  fontWeight: FontWeight.w500),
            ),
          ],
        ),
      ),
    );
  }
}
